<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/img/iconfont.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/img/iconfont.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/img/iconfont.png">
  <link rel="mask-icon" href="/img/iconfont.png" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.0/css/all.min.css" integrity="sha256-VHqXKFhhMxcpubYf9xiWdCiojEbY9NexQ4jh8AxbvcM=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"wang-weijun.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":true,"version":"8.26.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"codeblock":{"theme":{"light":"default","dark":"stackoverflow-dark"},"prism":{"light":"prism","dark":"prism-dark"},"copy_button":{"enable":false,"style":null},"fold":{"enable":false,"height":500},"language":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.json","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js" defer></script>

    <meta name="description" content="redis的学习">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis">
<meta property="og:url" content="http://wang-weijun.github.io/2023/05/17/Redis/index.html">
<meta property="og:site_name" content="Phils的杂货铺">
<meta property="og:description" content="redis的学习">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://wang-weijun.github.io/images/202408082155020.png">
<meta property="og:image" content="http://wang-weijun.github.io/images/202408082207171.png">
<meta property="og:image" content="http://wang-weijun.github.io/2023/05/17/images/202408082235448.png">
<meta property="og:image" content="http://wang-weijun.github.io/2023/05/17/images/202408082243413.png">
<meta property="article:published_time" content="2023-05-17T08:05:33.000Z">
<meta property="article:modified_time" content="2025-11-21T08:58:26.676Z">
<meta property="article:author" content="Phils">
<meta property="article:tag" content="Redis">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://wang-weijun.github.io/images/202408082155020.png">


<link rel="canonical" href="http://wang-weijun.github.io/2023/05/17/Redis/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://wang-weijun.github.io/2023/05/17/Redis/","path":"2023/05/17/Redis/","title":"Redis"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Redis | Phils的杂货铺</title>
  








  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous" defer></script>
<script src="/js/utils.js" defer></script><script src="/js/motion.js" defer></script><script src="/js/sidebar.js" defer></script><script src="/js/next-boot.js" defer></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.5.0/search.js" integrity="sha256-xFC6PJ82SL9b3WkGjFavNiA9gm5z6UBxWPiu4CYjptg=" crossorigin="anonymous" defer></script>
<script src="/js/third-party/search/local-search.js" defer></script>







  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Phils的杂货铺</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E8%A3%85"><span class="nav-number">1.</span> <span class="nav-text">安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%9A%E7%94%A8%E5%91%BD%E4%BB%A4%EF%BC%9A"><span class="nav-number">2.</span> <span class="nav-text">通用命令：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">3.</span> <span class="nav-text">常用数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#string-%E7%B1%BB%E5%9E%8B"><span class="nav-number">3.1.</span> <span class="nav-text">string 类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#set"><span class="nav-number">3.1.1.</span> <span class="nav-text">set</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#get"><span class="nav-number">3.1.2.</span> <span class="nav-text">get</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#incr"><span class="nav-number">3.1.3.</span> <span class="nav-text">incr</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#decr"><span class="nav-number">3.1.4.</span> <span class="nav-text">decr</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#list-%E7%B1%BB%E5%9E%8B"><span class="nav-number">3.2.</span> <span class="nav-text">list 类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E5%85%83%E7%B4%A0"><span class="nav-number">3.2.1.</span> <span class="nav-text">添加元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E5%85%83%E7%B4%A0"><span class="nav-number">3.2.2.</span> <span class="nav-text">查询元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E5%85%83%E7%B4%A0"><span class="nav-number">3.2.3.</span> <span class="nav-text">删除元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9%E5%85%83%E7%B4%A0"><span class="nav-number">3.2.4.</span> <span class="nav-text">修改元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%85%B6%E4%BB%96"><span class="nav-number">3.2.5.</span> <span class="nav-text">其他</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#set-%E7%B1%BB%E5%9E%8B"><span class="nav-number">3.3.</span> <span class="nav-text">set 类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9B%86%E5%90%88%E5%86%85"><span class="nav-number">3.3.1.</span> <span class="nav-text">集合内</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9B%86%E5%90%88%E9%97%B4"><span class="nav-number">3.3.2.</span> <span class="nav-text">集合间</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Hash-%E7%B1%BB%E5%9E%8B"><span class="nav-number">3.4.</span> <span class="nav-text">Hash 类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#zet-%E7%B1%BB%E5%9E%8B"><span class="nav-number">3.5.</span> <span class="nav-text">zet 类型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Redis"><span class="nav-number">4.</span> <span class="nav-text">Redis</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-conf-%E8%AF%A6%E8%A7%A3"><span class="nav-number">4.1.</span> <span class="nav-text">Redis.conf 详解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-%E6%8C%81%E4%B9%85%E5%8C%96"><span class="nav-number">4.2.</span> <span class="nav-text">Redis 持久化</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#RDB%EF%BC%88Redis-DataBase%EF%BC%89"><span class="nav-number">4.2.1.</span> <span class="nav-text">RDB（Redis DataBase）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#AOF%EF%BC%88Append-Only-File%EF%BC%89"><span class="nav-number">4.2.2.</span> <span class="nav-text">AOF（Append Only File）</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85"><span class="nav-number">4.3.</span> <span class="nav-text">Redis 发布订阅</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6"><span class="nav-number">4.4.</span> <span class="nav-text">Redis 主从复制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-%E7%BC%93%E5%AD%98%E7%A9%BF%E9%80%8F%E5%92%8C%E9%9B%AA%E5%B4%A9"><span class="nav-number">4.5.</span> <span class="nav-text">Redis 缓存穿透和雪崩</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Redis-%E6%95%B0%E6%8D%AE%E5%A4%87%E4%BB%BD%E4%B8%8E%E6%81%A2%E5%A4%8D"><span class="nav-number">5.</span> <span class="nav-text">Redis 数据备份与恢复</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Phils"
      src="/img/profile.png">
  <p class="site-author-name" itemprop="name">Phils</p>
  <div class="site-description" itemprop="description">个人博客，IT，技术分享</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">40</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">40</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/wang-weijun" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wang-weijun" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1191206969@qq.com" title="E-Mail → mailto:1191206969@qq.com" rel="noopener me" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://wang-weijun.github.io/2023/05/17/Redis/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/img/profile.png">
      <meta itemprop="name" content="Phils">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Phils的杂货铺">
      <meta itemprop="description" content="个人博客，IT，技术分享">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="Redis | Phils的杂货铺">
      <meta itemprop="description" content="redis的学习">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Redis
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-17 16:05:33" itemprop="dateCreated datePublished" datetime="2023-05-17T16:05:33+08:00">2023-05-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2025-11-21 16:58:26" itemprop="dateModified" datetime="2025-11-21T16:58:26+08:00">2025-11-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%8A%80%E6%9C%AF%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">技术栈</span></a>
        </span>
    </span>

  
</div>

            <div class="post-description">redis的学习</div>
        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>windows：</p>
<p><a target="_blank" rel="noopener" href="https://github.com/tporadowski/redis/releases">Releases · tporadowski&#x2F;redis (github.com)</a></p>
<p>点击安装<a target="_blank" rel="noopener" href="https://github.com/tporadowski/redis/releases/download/v5.0.14.1/Redis-x64-5.0.14.1.msi">Redis-x64-5.0.14.1.msi</a></p>
<h2 id="通用命令："><a href="#通用命令：" class="headerlink" title="通用命令："></a>通用命令：</h2><ul>
<li><p><strong>查看所有键</strong></p>
<p><code>keys</code>命令可用于查看所有键，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">keys pattern</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：pattern 用于匹配 key，其中<code>*</code>表示任意个任意字符，<code>?</code>表示一个任意字符。</p>
<p>示例：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; KEYS *</span><br><span class="line">1) &quot;k3&quot;</span><br><span class="line">2) &quot;k2&quot;</span><br><span class="line">3) &quot;k1&quot;</span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>：该命令会遍历 Redis 服务器中保存的所有键，因此当键很多时会影响整个 Redis 服务的性能，线上环境需要谨慎使用。</p>
</li>
<li><p><strong>键总数</strong></p>
<p><code>dbsize</code>可用于查看键的总数，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dbsize</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>判断键是否存在</strong></p>
<p><code>exists</code>命令可用于判断一个键是否存在，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">exists key</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：若键存在则返回 1，不存在则返回 0。</p>
</li>
<li><p><strong>删除键</strong></p>
<p><code>del</code>可用于删除指定键，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">del key [key ...]</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：返回值为删除键的个数，若删除一个不存在的键，则返回 0。</p>
</li>
<li><p><strong>查询键的剩余过期时间</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ttl key</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：<code>ttl</code>的含义为<strong>time to live</strong>，用于查询一个定时键的剩余存活时间，返回值以秒为单位。若查询的键的未设置过期时间，则返回<code>-1</code>，若查询的键不存在，则返回<code>-2</code>。</p>
</li>
<li><p><strong>数据库管理命令</strong></p>
<p>Redis 默认有编号为 0~15 的 16 个逻辑数据库，每个数据库之间的数据是相互独立的，所有连接默认使用的都是 0 号数据库。</p>
<ul>
<li><p><strong>切换数据库</strong></p>
<p><code>select</code>命令可用于切换数据库，语法如下</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select index</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：若 index 超出范围，会报错</p>
</li>
<li><p><strong>清空数据库</strong></p>
<p><code>flushdb</code>命令会清空当前所选用的数据库，<code>flushall</code>命令会清空 0~15 号所有的数据库。</p>
<p><strong>注意</strong>：生产环境慎用</p>
</li>
</ul>
</li>
</ul>
<h2 id="常用数据类型"><a href="#常用数据类型" class="headerlink" title="常用数据类型"></a>常用数据类型</h2><h3 id="string-类型"><a href="#string-类型" class="headerlink" title="string 类型"></a>string 类型</h3><p><strong>概述</strong>：Redis 中的 string 类型保存的是字节序列（Sequence of bytes），因此任意类型的数据，只要经过序列化之后都可以保存到 Redis 的 string 类型中，包括文本、数字甚至是一个对象。</p>
<h4 id="set"><a href="#set" class="headerlink" title="set"></a>set</h4><p><code>set</code>命令用于添加 string 类型的键值对，具体语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">set</span> key value [expiration EX seconds|PX milliseconds] [NX|XX]</span><br></pre></td></tr></table></figure>

<p>各选项含义：</p>
<ul>
<li><p>EX seconds：设置过期时间，单位为秒</p>
</li>
<li><p>PX milliseconds：设置过期时间，单位为毫秒</p>
</li>
<li><p>NX：仅在 key 不存在时 set</p>
</li>
<li><p>XX：仅在 key 存在时 set</p>
</li>
</ul>
<p>例如：<code>set k2 v2 ex 20</code>：设置 key 为 k2，value 为 v2，20s 后过期</p>
<h4 id="get"><a href="#get" class="headerlink" title="get"></a>get</h4><p><code>get</code>命令用于获取某个 string 类型的键对应的值，具体如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">get key</span><br></pre></td></tr></table></figure>

<h4 id="incr"><a href="#incr" class="headerlink" title="incr"></a>incr</h4><p><code>incr</code>命令用于对数值做自增操作，具体语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">incr key</span><br></pre></td></tr></table></figure>

<p>若 key 对应的 value 是整数，则返回自增后的结果，若不是整数则报错，若 key 不存在则创建并返回 1</p>
<h4 id="decr"><a href="#decr" class="headerlink" title="decr"></a>decr</h4><p><code>decr</code>命令用于对数值做自减操作，具体语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">decr key</span><br></pre></td></tr></table></figure>

<p><strong>应用场景</strong></p>
<p>string 类型常用于缓存、计数器等场景。</p>
<h3 id="list-类型"><a href="#list-类型" class="headerlink" title="list 类型"></a>list 类型</h3><p><strong>概述：</strong></p>
<p>list 类型可用于存储多个 string 类型的元素，并且所有元素按照被添加的顺序存储。</p>
<p><strong>常用命令</strong></p>
<p>list 类型相关的命令较多，下面分类进行进行介绍。</p>
<h4 id="添加元素"><a href="#添加元素" class="headerlink" title="添加元素"></a><strong>添加元素</strong></h4><p>向列表中添加元素的命令有<code>lpush</code>、<code>rpush</code> 、<code>linsert</code>，各命令的功能与用法如下</p>
<p><img src="/../images/202408082155020.png" alt="image-20240808215505935"></p>
<p><code>lpush</code>：该命令用于向 list 左侧添加元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lpush key value [value ...]</span><br></pre></td></tr></table></figure>

<p>示例：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lpush l1 a b c</span><br></pre></td></tr></table></figure>

<p><code>rpush</code>：该命令用于向 list 右侧添加元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rpush key value [value ...]</span><br></pre></td></tr></table></figure>

<p><code>linsert</code>：该命令用于向 list 指定位置添加元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">linsert key BEFORE|AFTER pivot value</span><br></pre></td></tr></table></figure>

<p>示例：在 li 的 list 列表中，在 b 后新增一个 value 为 new</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">linsert l1 after b new</span><br></pre></td></tr></table></figure>

<h4 id="查询元素"><a href="#查询元素" class="headerlink" title="查询元素"></a>查询元素</h4><p>查询 list 元素的命令有<code>lindex</code>和<code>lrange</code>，各命令的功能与用法如下</p>
<p><code>lindex</code>：该命令用于获取指定索引位置的元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lindex key index</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：index 从左到右依次是 0，1，2…，从右到左依次是-1，-2，-3…</p>
<p><code>lrange</code>：该命令用于获取指定范围内的元素列表，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lrange key start stop</span><br></pre></td></tr></table></figure>

<p>示例：获取 list 全部元素，命令如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lrange l1 0 -1</span><br></pre></td></tr></table></figure>

<h4 id="删除元素"><a href="#删除元素" class="headerlink" title="删除元素"></a>删除元素</h4><p>删除 list 元素的命令有<code>lpop</code>、<code>rpop</code>、<code>lrem</code>，各命令的功能与用法如下</p>
<p><img src="/../images/202408082207171.png" alt="image-20240808220734141"></p>
<p><code>lpop</code>：该命令用于移除并返回 list 左侧元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lpop key [count]</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：count 参数表示移除元素的个数</p>
<p><code>rpop</code>：该命令用于移除并返回 list 右侧的元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rpop key [count]</span><br></pre></td></tr></table></figure>

<p><code>lrem</code>：该命令用于移除 list 中的指定元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lrem key count value</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：count 参数表示要移除 element 元素的个数（list 中可以存在多个相同的元素），count 的用法如下</p>
<ul>
<li>若 count&gt;0，则从左到右删除最多 count 个 element 元素</li>
<li>若 count&lt;0，则从右到左删除最多 count（的绝对值）个 element 元素</li>
<li>若 count&#x3D;0，则删除所有的 element 元素</li>
</ul>
<h4 id="修改元素"><a href="#修改元素" class="headerlink" title="修改元素"></a>修改元素</h4><p><code>lset</code>：命令可用于修改指定索引位置的元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lset key index value</span><br></pre></td></tr></table></figure>

<h4 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h4><p><code>llen</code>命令可用于查看 list 长度，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">llen key</span><br></pre></td></tr></table></figure>

<p><strong>应用场景</strong></p>
<ul>
<li>社交应用中，可使用 list 缓存每个用户发布的最新的 N 条记录。</li>
<li>list 可用作异步消息队列。</li>
</ul>
<h3 id="set-类型"><a href="#set-类型" class="headerlink" title="set 类型"></a>set 类型</h3><p><strong>概述</strong>：和 list 类型相似，set 类型也可用来存储多个 string 类型的元素，但与 list 类型不同，set 中的元素是无序的，且 set 中不会包含相同元素。</p>
<h4 id="集合内"><a href="#集合内" class="headerlink" title="集合内"></a>集合内</h4><p><code>sadd</code>：该命令用于向 set 中添加元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sadd key member [member ...]</span><br></pre></td></tr></table></figure>

<p><code>smembers</code>：该命令用于查询 set 中的全部元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">smembers key</span><br></pre></td></tr></table></figure>

<p><code>srem</code>：该命令用于移除 set 中的指定元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">srem key member [member ...]</span><br></pre></td></tr></table></figure>

<p><code>spop</code>：该命令随机移除并返回 set 中的 n 个元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">spop key [count]</span><br></pre></td></tr></table></figure>

<p><code>srandmember：</code>该命令随机返回 set 中的 n 个元素（不删除），语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">srandmember key [count]</span><br></pre></td></tr></table></figure>

<p><code>scard</code>：该命令用于查询 set 中的元素个数，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scard key</span><br></pre></td></tr></table></figure>

<p><code>sismember</code>：该命令用于元素是否在 set 中，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sismember key member</span><br></pre></td></tr></table></figure>

<h4 id="集合间"><a href="#集合间" class="headerlink" title="集合间"></a>集合间</h4><p><code>sinter</code>：该命令用于计算多个集合的交集，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sinter key [key ...]</span><br></pre></td></tr></table></figure>

<p><code>sunion</code>：该命令用于计算多个集合的并集，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sunion key [key ...]</span><br></pre></td></tr></table></figure>

<p><code>sdiff</code>：该命令用于计算多个集合的差集，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sdiff key [key ...]</span><br></pre></td></tr></table></figure>

<p><strong>应用场景</strong></p>
<p>set 可用于计算共同关注好友，随机抽奖系统等等。</p>
<h3 id="Hash-类型"><a href="#Hash-类型" class="headerlink" title="Hash 类型"></a>Hash 类型</h3><p><strong>概述</strong>：hash 类型类似于 Java 语言中的<code>HashMap</code>，可用于存储键值对。</p>
<img src="../images/202408082235448.png" style="zoom:50%" />

<p><code>hset</code>：该命令用于向 hash 中增加键值对，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hset key field value [field value ...]</span><br></pre></td></tr></table></figure>

<p><code>hget</code>：该命令用于获取 hash 中某个键对应的值，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hget key field</span><br></pre></td></tr></table></figure>

<p><code>hdel</code>：该命令用于删除 hash 中的指定的键值对，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hdel key field [field ...]</span><br></pre></td></tr></table></figure>

<p><code>hlen</code>：该命令用于查询 hash 中的键值对个数，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hlen key</span><br></pre></td></tr></table></figure>

<p><code>hexists</code>：该命令用于判断 hash 中的某个键是否存在，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexists key field</span><br></pre></td></tr></table></figure>

<p><code>hkeys</code>：该命令用于返回 hash 中所有的键，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hkeys key</span><br></pre></td></tr></table></figure>

<p><code>hvals</code>：该命令用于返回 hash 中所有的值，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hvals key</span><br></pre></td></tr></table></figure>

<p><code>hgetall</code>：该命令用于返回 hash 中所有的键与值，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hgetall key</span><br></pre></td></tr></table></figure>

<p><strong>应用场景</strong></p>
<p>hash 类型可用于缓存对象等。</p>
<h3 id="zet-类型"><a href="#zet-类型" class="headerlink" title="zet 类型"></a>zet 类型</h3><p><strong>概述</strong>：zset（sorted set）被称为有序集合，同 set 相似，zset 中也不会包含相同元素，但不同的是，zset 中的元素是有序的。并且 zset 中的元素并非像 list 一样按照元素的插入顺序排序，而是按照每个元素的分数（score）排序。</p>
<img src="../images/202408082243413.png" style="zoom:50%;" />

<p><code>zadd</code>：该命令用于向 zset 中添加元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zadd key [NX|XX] [CH] [INCR] score member [score member ...]</span><br></pre></td></tr></table></figure>

<p>**说明***：</p>
<ul>
<li>NX：仅当 member 不存在时才 add</li>
<li>XX：仅当 member 存在时才 add</li>
<li>CH：返回此次操作后有序集合元素发生变化的数量</li>
<li>INCR：如果 member 存在，其分数会在原分数的基础上增加指定的增量值；如果成员不存在，则会添加该成员，并将其分数设置为增量值。</li>
</ul>
<p><code>zcard</code>：该命令用于计算 zset 中的元素个数，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zcard key</span><br></pre></td></tr></table></figure>

<p><code>zscore</code>：用于查看某个元素的分数，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zscore key member</span><br></pre></td></tr></table></figure>

<p><code>zrank/zrevrank</code>：这组命令用于计算元素的排名，其中 zrank 按照 score 的升序排序，zrevrank 则按照降序排序，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zrank/zrevrank key member</span><br></pre></td></tr></table></figure>

<p><strong>说明：</strong>名次从 0 开始。</p>
<p><code>zrem</code>：该命令用于删除元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zrem key member [member ...]</span><br></pre></td></tr></table></figure>

<p><code>zincrby</code>：该命令用于增加元素的分数，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zincrby key increment member</span><br></pre></td></tr></table></figure>

<p><code>zrange</code>：该命令用于查询指定区间范围的元素，语法如下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zrange key start stop [byscore] [rev] [<span class="built_in">limit</span> offset count] [withscores]</span><br></pre></td></tr></table></figure>

<p><strong>说明</strong>：</p>
<ul>
<li>start&#x2F;stop：用于指定查询区间，但是在不同模式下，其代表的含义也不相同<ul>
<li>默认模式下，<code>start~stop</code>表示的是名次区间，且该区间为闭区间。名次从 0 开始，且可为负数，-1 表示倒数第一，-2 表示倒数第二，以此类推。</li>
<li>byscore 模式下（声明了 byscore 参数），则<code>start~stop</code>表示的就是分数区间，该区间默认仍为闭区间。在该模式下，可以在<code>start</code>或<code>stop</code>前增加<code>(</code>来表示开区间，例如<code>(1 (5</code>，表示的就是<code>(1,5)</code>这个开区间。除此之外，还可以使用<code>-inf</code>和<code>+inf</code>表示负无穷和正无穷。</li>
</ul>
</li>
<li>byscore：用于切换到分数模式</li>
<li>rev：表示降序排序。在 byscore 模式下使用 rev 参数需要注意查询区间，start 应大于 stop。</li>
<li>limit：该选项只用于 byscore 模式，作用和 sql 语句中的 limit 一致</li>
<li>withscores：用于打印分数</li>
</ul>
<p><strong>应用场景</strong></p>
<p>zset 主要用于各种排行榜。</p>
<h2 id="Redis"><a href="#Redis" class="headerlink" title="Redis"></a>Redis</h2><h3 id="Redis-conf-详解"><a href="#Redis-conf-详解" class="headerlink" title="Redis.conf 详解"></a>Redis.conf 详解</h3><h3 id="Redis-持久化"><a href="#Redis-持久化" class="headerlink" title="Redis 持久化"></a>Redis 持久化</h3><p>Redis 是内存数据库，如果不将内存中的数据库状态保存到磁盘，那么一旦服务器进程退出，服务器中的数据库状态也会消失，所以 Redis 提供了持久化功能！</p>
<h4 id="RDB（Redis-DataBase）"><a href="#RDB（Redis-DataBase）" class="headerlink" title="RDB（Redis DataBase）"></a>RDB（Redis DataBase）</h4><blockquote>
<p>什么是 RDB</p>
</blockquote>
<h4 id="AOF（Append-Only-File）"><a href="#AOF（Append-Only-File）" class="headerlink" title="AOF（Append Only File）"></a>AOF（Append Only File）</h4><p>将我们的所有命令都记录下来，history，恢复的时候就把这个文件全部执行一遍！</p>
<blockquote>
<p>AOF</p>
</blockquote>
<h3 id="Redis-发布订阅"><a href="#Redis-发布订阅" class="headerlink" title="Redis 发布订阅"></a>Redis 发布订阅</h3><p>Redis 发布订阅（pub&#x2F;sub）是一种&#x3D;&#x3D;消息通信模式&#x3D;&#x3D;：发送者（pub），订阅者（sub）接收消息。</p>
<p>Redis 客户端可以订阅任意数量的频道。</p>
<h3 id="Redis-主从复制"><a href="#Redis-主从复制" class="headerlink" title="Redis 主从复制"></a>Redis 主从复制</h3><h3 id="Redis-缓存穿透和雪崩"><a href="#Redis-缓存穿透和雪崩" class="headerlink" title="Redis 缓存穿透和雪崩"></a>Redis 缓存穿透和雪崩</h3><h2 id="Redis-数据备份与恢复"><a href="#Redis-数据备份与恢复" class="headerlink" title="Redis 数据备份与恢复"></a>Redis 数据备份与恢复</h2><p><strong>备份数据</strong></p>
<p>redis Save 命令基本语法如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; SAVE</span><br><span class="line">OK</span><br></pre></td></tr></table></figure>

<p>该命令将在 redis 安装目录中创建 dump.rdb 文件。</p>
<p><strong>恢复数据</strong></p>
<p>登录目标 redis 服务器，我们先<strong>停止 redis 服务</strong>：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">service redis stop  <span class="comment">#停止redis服务</span></span><br></pre></td></tr></table></figure>

<p>然后进入 redis 的文件存放目录 redis,把刚刚备份的 dump.rdb 文件替换该目录下的 dump.rdb 文件（建议先备份当前目录下的 dump.rdb 文件），<strong>重启 redis 服务</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">service redis start <span class="comment">#启动redis服务</span></span><br></pre></td></tr></table></figure>

<p>到此，redis 数据迁移完成</p>
<blockquote>
<p>获取</p>
</blockquote>
<p>获取 redis 目录可以使用 <strong>CONFIG</strong> 命令，如下所示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis 127.0.0.1:6379&gt; CONFIG GET <span class="built_in">dir</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Redis/" rel="tag"># Redis</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/05/17/Python%E5%B7%A5%E5%85%B7%E7%B1%BB/" rel="prev" title="Python工具类">
                  <i class="fa fa-angle-left"></i> Python工具类
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/05/17/ChatGPT/" rel="next" title="ChatGPT">
                  ChatGPT <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Phils</span>
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>

</body>
</html>
